package cn.com.css.taxInspection.workflow.service.impl;

import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.gt3.entity.JcAjxxEntity;
import cn.com.css.taxInspection.gt3.service.DmGySwjgService;
import cn.com.css.taxInspection.gt3.service.DmGySwryService;
import cn.com.css.taxInspection.gt3.service.JcAjxxService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.DateUtils;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workflow.constants.WorkflowConstants;
import cn.com.css.taxInspection.workflow.entity.AjGzddEntity;
import cn.com.css.taxInspection.workflow.entity.GzddAjContentEntity;
import cn.com.css.taxInspection.workflow.service.GzddAjContentService;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.css.taxInspection.workflow.entity.WorkflowLogEntity;
import cn.com.css.taxInspection.workflow.mapper.WorkflowLogMapper;
import cn.com.css.taxInspection.workflow.service.WorkflowLogService;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service("workflowLogService")
public class WorkflowLogServiceImpl extends ServiceImpl<WorkflowLogMapper, WorkflowLogEntity> implements WorkflowLogService {

    @Resource
    WorkflowLogMapper workflowLogMapper;

    @Resource
    HttpSessionService httpSessionService;

    @Resource
    DmGySwryService dmGySwryService;

    @Resource
    DmGySwjgService dmGySwjgService;

    @Resource
    GzddAjContentService gzddAjContentService;
    @Autowired
    private JcAjxxService jcAjxxService;

    @Override
    public void deleteDataById(String id) {
        workflowLogMapper.deleteDataById(id);
    }


    // 保存一键工作调度信息
    @Override
    @DS("oracle")
    public boolean saveYjdd(AjGzddEntity ajGzddEntity) throws ParseException {
        //获取当前登录税务人员代码
        String currentUserId = httpSessionService.getCurrentUserId();
        //模板id
        String mold = ajGzddEntity.getMold();
        WorkflowLogEntity workflowLogEntity = new WorkflowLogEntity();
        String workflowLogId = TsBqUtils.getUUID();
        workflowLogEntity.setId(workflowLogId);
        //调度类型.默认为保存 0 为 市级保存 1 为 局级保存   一键调度均以局级单位为主。
        workflowLogEntity.setType(Constants.GZDD_DD_PUSH);
        // 模板类型
        String cate = WorkflowConstants.LOG_CATE_1 + "," + WorkflowConstants.LOG_CATE_2 + "," + WorkflowConstants.LOG_CATE_3;
        workflowLogEntity.setCate(cate);
        workflowLogEntity.setDeleted(Constants.DELETEED_NOT);
        //创建人
        workflowLogEntity.setCreateId(currentUserId);
        //创建时间
        workflowLogEntity.setCreateTime(DateUtils.getCurrentDate());
        //创建时间
        workflowLogEntity.setContentdate(DateUtils.getCurrentDate());
        //模板ID
        workflowLogEntity.setMold(mold);
        //创建人
        workflowLogEntity.setTaxPerson(currentUserId);
        //调度名称 一键调度 这里存案件名称
        JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(ajGzddEntity.getJcajxxuuid());
        workflowLogEntity.setDdmc(jcAjxxEntity.getAjmc());
        //稽查案件信息UUID合集  一件调度 单条jcajxxuuid
        workflowLogEntity.setJcajxxuuids(ajGzddEntity.getJcajxxuuid());
        //创建人名称
        workflowLogEntity.setCreateName(currentUserId + "|" + dmGySwryService.getSwry(currentUserId).getSwryxm());
        //保存工作调度-案件列表内容信息
        saveGzddAjContent(ajGzddEntity, workflowLogId);
        // 写入确认表
        return this.save(workflowLogEntity);
    }

    //保存工作调度-案件列表内容信息
    public void saveGzddAjContent(AjGzddEntity ajGzddEntity, String workflowLogId) {
        List<String> ddjsryList = combineDdjsry(ajGzddEntity);
        for (String s : ddjsryList) {
            //保存案件调度-案件内容信息
            GzddAjContentEntity gzddAjContentEntity = new GzddAjContentEntity();
            gzddAjContentEntity.setUuid(TsBqUtils.getUUID());
            gzddAjContentEntity.setDduuid(workflowLogId);
            gzddAjContentEntity.setJcajxxuuid(ajGzddEntity.getJcajxxuuid());
            gzddAjContentEntity.setJcajbh(ajGzddEntity.getJcajbh());
            gzddAjContentEntity.setCreateTime(TsBqUtils.getSystemTime());
            gzddAjContentEntity.setDdjsry(s);
            gzddAjContentEntity.setAjcontent(ajGzddEntity.getAjContent());
            gzddAjContentEntity.setDdzt("未填报");
            gzddAjContentEntity.setBz(5);
            gzddAjContentService.save(gzddAjContentEntity);
        }
    }

    //去掉中括号
    private String cleanZdrString(String zdrStr) {
        if (zdrStr.startsWith("[") && zdrStr.endsWith("]")) {
            return zdrStr.substring(1, zdrStr.length() - 1).trim();
        }
        return zdrStr;
    }

    public List<String> combineDdjsry(AjGzddEntity ajGzddEntity) {
        List<String> resultList = new ArrayList<>();

        if (ajGzddEntity.getJcry() != null && !ajGzddEntity.getJcry().trim().isEmpty()
                && !"无".equalsIgnoreCase(ajGzddEntity.getJcry().trim())) {
            resultList.addAll(Arrays.asList(ajGzddEntity.getJcry().trim().split(",")));
        }

        if (ajGzddEntity.getSlry() != null && !ajGzddEntity.getSlry().trim().isEmpty()
                && !"无".equalsIgnoreCase(ajGzddEntity.getSlry().trim())) {
            resultList.addAll(Arrays.asList(ajGzddEntity.getSlry().trim().split(",")));
        }

        if (ajGzddEntity.getZxry() != null && !ajGzddEntity.getZxry().trim().isEmpty()
                && !"无".equalsIgnoreCase(ajGzddEntity.getZxry().trim())) {
            resultList.addAll(Arrays.asList(ajGzddEntity.getZxry().trim().split(",")));
        }

        if (TsBqUtils.isNotNull(ajGzddEntity.getZdr())) {
            String zdr = cleanZdrString(ajGzddEntity.getZdr());
            if (!zdr.trim().isEmpty()) {
                // 去掉引号并按 , 拆分
                String[] parts = zdr.split(",");
                for (String part : parts) {
                    String cleaned = part.replaceAll("\"", "").trim();
                    if (!cleaned.isEmpty()) {
                        resultList.add(cleaned);
                    }
                }
            }
        }

        return resultList;
    }


}